﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Business;
using NetDon.OA.Interface;
using NetDon.OA.Interface.Admin;
using NetDon.OA.Portal.Admin;
using NetDon.OA.Portal.Constants;
using NetDon.Utilities;
using NetDon.WebControls;
using NineRays.WebControls;

namespace NetDon.OA.Portal.Controls
{
    public partial class Selector : PageBase
    {
        Guid? CompanyId
        {
            get
            {
                return treeOrganization.CompanyId;
            }
        }

        Guid DepartmentId
        {
            get
            {

                return treeOrganization.DepartmentId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            treeOrganization.LoadItems += this.btnSearch_Click;
            if (!IsPostBack)
            {
                CancelButton = btnCancel;
                PagerUsers.PageSize = 100;
                dplUserStatus.SelectedValue = UserStatus.Active.ToString();
                PagerUsers.PerformQuery();
            }
        }

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            gridUsers.Visible = true;
            PagerUsers.PerformQuery();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PagerUsers.PerformQuery();
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            PagerUsers.ReloadCurrentPage();
        }

        protected void gridUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);
                if (entity != null)
                {
                    e.Row.Attributes["UserId"] = Convert.ToString(DataBinder.Eval(entity, "UserId"));
                    e.Row.Attributes["UserName"] = Convert.ToString(DataBinder.Eval(entity, "UserName"));
                }
            }
        }

        protected void PagerUsers_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            var service = ServiceContainer.GetService<IUserService>();
            e.DataSource = service.SearchUsers((UserSearchCriteria)e.Condition,
                gridUsers.SortExpression, gridUsers.SortDirection, e.PageSize, e.PageIndex);
        }

        protected void PagerUsers_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var service = ServiceContainer.GetService<IUserService>();
            e.RecordCount = service.GetUserCount((UserSearchCriteria)e.Condition);
        }

        protected void PagerUsers_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var criteria = new UserSearchCriteria
            {
                UserName = txtLoginName.Text.Trim(),
                Email = txtEmail.Text.Trim(),
                FullName = txtFullName.Text.Trim()
            };

            if (!string.IsNullOrEmpty(dplUserType.SelectedValue))
            {
                criteria.UserType = dplUserType.SelectedValue.ToEnum<UserType>();
            }

            if (!string.IsNullOrEmpty(dplUserStatus.SelectedValue))
            {
                criteria.Status = dplUserStatus.SelectedValue.ToEnum<UserStatus>();
            }
            if (CompanyId.HasValue && CompanyId.Value != Guid.Empty)
            {
                criteria.CompanyId = CompanyId;
            }
            if (DepartmentId != Guid.Empty)
            {
                criteria.DepartId = DepartmentId;
            }
            e.Condition = criteria;
        }

        protected void btnChoose_Click(object sender, EventArgs e)
        {

        }
    }
}